from flask import Blueprint, jsonify, request
from models.entity.gps_device import Gps_device
import json
from utils.result import result

mod = Blueprint("core_gps_device", __name__, url_prefix="/core/gps_device", template_folder="templates",
                static_folder="static")


@mod.route('/add/', methods=['POST'])
def add_gps_device():
    """增加gps装备"""
    """
    {
    "name":"光线传感器",
    "code":"256412X",
    "description":"wu",
    "sensor_type_id":1,
    "sensor_group_id":1,
    "device_id":1,
    "dis_order":1
    }
    """
    try:
        dict = json.loads(request.data.decode("utf-8"))
        print(dict)
    except:
        return result(success=0, error_code=-1, error_msg="json格式错误")
    if "id" in dict:
        dict.pop("id")
    gps_device = Gps_device()
    if Gps_device.query.filter_by(name=dict["name"]).first() is not None:
        return result(success=0, error_code=-2, error_msg="名称已存在")
    if Gps_device.query.filter_by(code=dict["code"]).first():
        return result(success=0, error_code=-2, error_msg="编码已存在")
    print(11111)
    gps_device.code = dict['code']
    gps_device.name = dict['name']
    gps_device.phone_num = dict['phone_num']
    gps_device.plate_num = dict['plate_num']
    gps_device.sim_num = dict['sim_num']
    gps_device.department = dict['department']
    if gps_device.save():
        print(222222)
        return result(success=1)
    else:
        return result(success=0, error_code=-3, error_msg="未知错误")


@mod.route('/edit/', methods=['POST'])
def edit_gps_device():
    """编辑gps装备"""
    """
    {
    "id":1,
    "name":"气器",
    "code":"HDKddL",
    "description":"wu",
    "dis_order":2
    }
    """
    try:
        dict = json.loads(request.data.decode("utf-8"))
    except:
        return result(success=0, error_code=-1, error_msg="json格式错误")
    gps_device = Gps_device.query.filter_by(id=dict["id"]).first()
    # if Gps_device.query.filter_by(name=dict["name"]).first() is not None:
    #     return result(success=0, error_code=-2, error_msg="名称已存在")
    # if Gps_device.query.filter_by(code=dict["code"]).first():
    #     return result(success=0, error_code=-2, error_msg="编码已存在")
    print(1111)
    gps_device.code = dict['code']
    gps_device.name = dict['name']
    gps_device.phone_num = dict['phone_num']
    gps_device.plate_num = dict['plate_num']
    gps_device.sim_num = dict['sim_num']
    print(22)
    gps_device.department = dict['department']
    print(33)
    if gps_device.save():
        return result(success=1)
    else:
        return result(success=0, error_code=-3, error_msg="未知错误")


@mod.route('/del/', methods=['POST'])
def del_gps_device():
    """删除gps装备"""
    """
    {
    "id":1
    }
    """
    try:
        dict = json.loads(request.data.decode("utf-8"))
    except:
        return result(success=0, error_code=-1, error_msg="json格式错误")
    gps_device = Gps_device.query.filter_by(id=int(dict["id"])).first()
    if gps_device.delete():
        return result(success=1)
    else:
        return result(success=0, error_code=-3, error_msg="未知错误")


@mod.route('/get_all/', methods=['GET', 'POST'])
def get_gps_device():
    """获取所有gps装备"""
    """
        无需传入参数
    """
    gps_devices = Gps_device.get_gps_device_all()
    t_list = [item.get_dic() for item in gps_devices]
    return result(success=1, data=t_list)

@mod.route("/search/",methods=["POST"])
def search_gps_device():
    """
    模糊查询
    """
    # request: json
    # {
    #      "query_word": "lishimin"
    # }
    try:
        dict = json.loads(request.data.decode("utf-8"))
    except:
        return result(success=0, error_code=-1, error_msg="json格式错误")
    tmp ={}
    data =[]
    if 'query_word' in dict:
        query_word = dict['query_word']
    else:
        tmp['success'] = 0
        tmp['error_code'] = -9
        tmp['error_msg'] = "数据格式错误"
        tmp['data'] = data
        return jsonify(tmp)
    gps_device = Gps_device.search(query_word)
    return jsonify(gps_device)
